//
//  BMapViewController.m
//  BSLPlugin
//
//  Created by foreveross on 12/10/15.
//  Copyright (c) 2015 Fanty. All rights reserved.
//

#import "BMapViewController.h"

@interface BMapViewController ()
{
    BMKMapView *_bmkView;
    BMKLocationService *_locService;
    BMKPointAnnotation *_PointAnnotation;
     CLLocationCoordinate2D currentUserLocation ;
    BMKPolyline *_lastPolyline;
    NSMutableArray *lineArray ;
}
@end

@implementation BMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    lineArray = [[NSMutableArray alloc]initWithCapacity:0];
    if ([[UIDevice currentDevice].systemVersion floatValue]>=8) {
        CLLocationManager *locationManger = [[CLLocationManager alloc]init];
        [locationManger requestAlwaysAuthorization];
        [locationManger requestWhenInUseAuthorization];
    }
    
    _bmkView = [[BMKMapView alloc]initWithFrame:self.view.frame];
    _bmkView.zoomLevel = 12;
    _bmkView.mapType = BMKMapTypeStandard;
    
   
    [self.view addSubview:_bmkView];
    
   
    // 创建一个定位对象
    _locService = [[BMKLocationService alloc] init];
    // 开始定位
    [_locService startUserLocationService];
    //设置定位的状态
    _bmkView.userTrackingMode = BMKUserTrackingModeNone;
    
    
   
}
-(void)viewWillAppear:(BOOL)animated {
    [_bmkView viewWillAppear];
    _bmkView.delegate = self;
    _locService.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated {
    [_bmkView viewWillDisappear];
    _bmkView.delegate = nil; 
    _locService.delegate = nil;
}
- (void)viewDidAppear:(BOOL)animated
{
    // 添加一个PointAnnotation
    _PointAnnotation = [[BMKPointAnnotation alloc]init];
    
//    _PointAnnotation.coordinate = _coor;
    _PointAnnotation.title = @"目标位置";
    
    [_bmkView addAnnotation:_PointAnnotation];
}


- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    
   _bmkView.centerCoordinate = userLocation.location.coordinate;
    
   _bmkView.showsUserLocation = YES;//显示定位图层
    [_bmkView updateLocationData:userLocation];
    [_locService stopUserLocationService];
    
    currentUserLocation = userLocation.location.coordinate;

}

//两点直接的连线
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
    [_bmkView removeOverlay:_lastPolyline];
     if ([overlay isKindOfClass:[BMKPolyline class]]){
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay] ;
        polylineView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1];
        polylineView.lineWidth = 5.0;

        return polylineView;
    }
    return nil;
}
//点击当前位置获得信息
- (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi*)mapPoi{
     NSLog(@"点击= %@: 经度= %f, 纬度= %f ",mapPoi.text, mapPoi.pt.latitude,mapPoi.pt.longitude);
    
    _PointAnnotation.coordinate = mapPoi.pt;

    
    
    //延迟划线
    [self performSelector:@selector(addLocationToDrawingLine) withObject:nil afterDelay:0.0];
}

- (void)addLocationToDrawingLine
{
    // 添加折线覆盖物
    CLLocationCoordinate2D coors[2] = {0};
    coors[0] = currentUserLocation;
    coors[1] = _PointAnnotation.coordinate;
    BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coors count:2];
   
    [_bmkView addOverlay:polyline];
    [lineArray addObject:polyline];
    if (lineArray.count == 2) {
    _lastPolyline = [lineArray firstObject];
        [lineArray removeObject:[lineArray objectAtIndex:0]];
    }
 
}






- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
